<script setup lang="ts">
import useSettingsStore from '@/store/modules/settings.ts'
import useUserStore from '@/store/modules/user'
import { loadingHide, loadingShow } from '@/ui/components/FaLoading'
import { useRoute, useRouter } from 'vue-router'

// 新增路由hook
const type = ref<'plane' | 'chase' | 'bounce' | 'wave' | 'pulse' | 'flow' | 'swing' | 'circle' | 'circle-fade' | 'grid' | 'fold' | 'wander'>('chase')
const size = ref(100)
const text = ref('微信登录中...')
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const route = useRoute() // 获取路由实例
const router = useRouter()
const redirect = ref(route.query.redirect?.toString() ?? settingsStore.settings.home.fullPath)

function loading() {
  // 提供 3 个参数
  loadingShow({
    type: type.value,
    size: size.value,
    text: text.value,
  })
}
console.log(redirect)
// 获取路由参数的两种方式：
async function getRouteParams() {
  // 方式1：获取query参数（适用于?key=value形式）
  console.log('URL查询参数:', route.query)
  // 方式2：获取params参数（适用于/route/:id形式）
  console.log('路由路径参数:', route.params)

  if (route.query.code) {
    const authCode = route.query.code
    const state = route.query.state
    console.log('微信授权码:', authCode)
    const loginData: any = {
      code: authCode,
    }
    await userStore.wxlogin(loginData).then((res: any) => {
      if (res.code === 200 && res.data.status === '1') {
        // 直接登录
        router.push(redirect.value)
      }
      else if (res.code === 200 && res.data.status === '2') {
        // 跳转到绑定手机页面
        router.push('/bindPhone')
      }
      else {
        router.push('/login')
      }
    })
  }
}
onMounted(() => {
  loading()
  getRouteParams()
})
onUnmounted(() => {
  loadingHide()
})
</script>

<template>
  <div />
</template>
